home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_sound.lha / Sound / LIB_Memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-18  |  2.8 KB  |  97 lines

  1.  
  2. #ifdef MACHINE_AMIGA
  3.  #include <clib/exec_protos.h>
  4.  #include <exec/memory.h>
  5. #endif
  6.  
  7. #define MEMHEADER    20             /* 16 bytes at start, 4 at end */
  8. #define MEMF_REVERSE (1L<<18)
  9. #define CODE_MEMH    0x4D454D48L
  10. #define CODE_MEMT    0x4D454D54L
  11.  
  12. /***********************************************************************************
  13. ** Function: AllocSoundMem()
  14. ** Synopsis: Memory = AllocSoundMem(Size, Flags);
  15. **
  16. ** Allocates memory that can be used to play audio samples.
  17. */
  18.  
  19. LIBFUNC APTR LIBAllocSoundMem(mreg(__d0) LONG Size, mreg(__d1) LONG Flags)
  20. {
  21.    struct DPKTask *Task;
  22.    LONG *Memory, *EndMemory, AFlags;
  23.    WORD i;
  24.  
  25.    if (Size > NULL) {
  26.       if (Size & 0x00000001) {     /* Check if uneven, if so raise the size */
  27.          Size++;
  28.       }
  29.  
  30.       Size  += MEMHEADER;           /* ++MEMHEADER */
  31.       AFlags = MEMF_CHIP;           /* Clear, Chip */
  32.  
  33.       if ((Flags & MEM_NOCLEAR) IS NULL) {
  34.          AFlags |= MEMF_CLEAR;
  35.       }
  36.  
  37.       if (Size > 32767) {           /* Large chunks go to the other side of the  */
  38.          AFlags |= MEMF_REVERSE;    /* memory boundary to prevent fragmentation. */
  39.       }
  40.  
  41.       /* Allocate the memory using the exec.library
  42.       ** routines.
  43.       */
  44.  
  45.       if (Memory = AllocMem(Size,AFlags)) {
  46.          Task = FindDPKTask();
  47.  
  48.          if (Task IS NULL) {
  49.             Flags |= MEM_UNTRACKED;
  50.          }
  51.  
  52.          EndMemory    = (LONG *)(((BYTE *)Memory)+Size-4);
  53.          EndMemory[0] = CODE_MEMT;
  54.  
  55.          i = NULL;
  56.          Memory[i++] = Flags;            /* Remember memory type */
  57.          Memory[i++] = Size-MEMHEADER;   /* Remember size */
  58.  
  59.          if ((Flags & MEM_UNTRACKED) OR (Task IS NULL)) {
  60.             if (Memory[i++] = (LONG)GVBase->SystemTask) {
  61.                AddResource(GVBase->SystemTask, RSF_MEMORY, (APTR)(Memory + 4));
  62.                GVBase->SystemTask->Head.Stats->TotalSound += Size;
  63.             }
  64.          }
  65.          else {
  66.             if (Task->prvContext) {
  67.                AddResource(Task->prvContext, RSF_MEMORY, (APTR)(Memory + 4));
  68.                Memory[i++] = (LONG)Task->prvContext;
  69.                Task->prvContext->Stats->TotalSound += Size;
  70.             }
  71.             else DPrintF("!AllocMemBlock:","This Task has no context!");
  72.          }
  73.  
  74.          Memory[i++] = CODE_MEMH;        /* Memory header */
  75.  
  76.          if (GVBase->Debug) GVBase->Debug->AllocSoundMem(Size,Flags,Memory+i);
  77.          StepBack();
  78.          return(Memory+i);
  79.       }
  80.       else DPrintF("AllocSoundMem()","Could not allocate memory space.");
  81.    }
  82.    else DPrintF("AllocSoundMem()","You requested a memory size of NULL.");
  83.  
  84.    StepBack();
  85.    return(NULL);
  86. }
  87.  
  88. /***********************************************************************************
  89. ** Function: FreeSoundMem()
  90. */
  91.  
  92. LIBFUNC LONG LIBFreeSoundMem(mreg(__d0) APTR MemBlock)
  93. {
  94.   return(FreeMemBlock(MemBlock));
  95. }
  96.  
  97.